package org.WhyMCA.Adapters;

/**
@author Danilo '' Riso >> danilo DOT riso AT gmail DOT com
* @author Androidiani Dev Team
* @version 1.0
*/

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

import org.xmlpull.v1.XmlPullParserException;

import org.WhyMCA.WhyMCANews.R;
import org.WhyMCA.WhyMCANews.ViewPost;
import org.WhyMCA.Models.WhyMCANewsModel;
import org.WhyMCA.Utils.DialogMgr;
import org.WhyMCA.Utils.Global;
import org.WhyMCA.Utils.PreferencesMgr;
import org.WhyMCA.Utils.DialogMgr.DialogCallback;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.DataSetObserver;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.AnimationDrawable;
import android.net.Uri;
import android.os.Environment;
import android.os.Handler;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Adapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 *  @author Danilo '' Riso >> danilo DOT riso AT gmail DOT com
 * @author danilo
 *
 */

/**
 * Main Activity UI interface
 * @author danilo
 *
 */

public class WhyMCAAdapter implements ListAdapter, OnClickListener, DialogCallback {
	
	private ArrayList<WhyMCANewsModel> _nl;
	private Context context;
	private View _row;
	private Intent _viewpost;
    private WhyMCANewsModel toShowIL, toShowCent, toShowBot, toShowOnce;
    private static final int START = 0;
    private static final int CENTER = 1;
    private static final int END = 2;
    private static final int ONCE = 3;
 	
	/**
	 * Contiene la bitmap di profilo
	 */
	
	
	//public WeatherMainAdapterNew (Context context, ArrayList<CurrentConditionsModel> cc, ArrayList<ExpandedForecastModel> ed) {
		public WhyMCAAdapter (Context context, ArrayList<WhyMCANewsModel> nl) {
		this._nl = nl;
		this.context = context;
		
	}

	public boolean areAllItemsEnabled() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isEnabled(int position) {
		return false;
	}

	public int getCount() {
		return (this._nl.size());
		
	}

	public Object getItem(int position) {
			return _nl.get(position);
		
	}

	public long getItemId(int position) {
		return position;
	}

	public int getItemViewType(int position) {

		if(position == 0)
			
			return START;
			
		if(position == (getCount() - 1))
			
			return END;
		
		if(getCount() == 1)
			
			return ONCE;
			
		else
			
			return CENTER;
				
	}
	
	public int getViewTypeCount() {
		return 3;
		
	}

	public boolean hasStableIds() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isEmpty() {
		return false;
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		// TODO Auto-generated method stub
		
	}
			
	public View getView(final int position, View convertView, ViewGroup parent) {
			
		
		int _type = getItemViewType(position);
        //System.out.println("getView " + position + " " + convertView + " type = " + _type);
        _row = convertView;
                  
        	System.out.println("Sono in row");
			LayoutInflater inflater=((Activity) this.context).getLayoutInflater();
        
        switch (_type) {
        			
        	case START:
        		
        			_row = inflater.inflate(R.layout.news_row_top, null, false);
        	
                    LinearLayout _citysearchin = (LinearLayout)_row.findViewById(R.id.LinearLayoutSearchList);
            		TextView _titlein = (TextView)_row.findViewById(R.id.TextViewTitle);
            		TextView _datein = (TextView)_row.findViewById(R.id.TextViewPubDate);
            		//ImageView _imagetypein = (ImageView)_row.findViewById(R.id.ImageView);
            		            		            		
            		System.out.println("Position: " + position);
            		toShowIL = ((WhyMCANewsModel)this.getItem(position));
            		_titlein.setText(toShowIL.getTitle());
            		_datein.setText(toShowIL.getDataTrunk());
            		            		            		
            		_citysearchin.setOnClickListener(new OnClickListener(){

        			@Override
        			public void onClick(View v) {
        					
        				addSearchPreferences(position);
        					        					 
        				}
        				
        			});
            		
            		break;
            
        	case CENTER:
        		
    			_row = inflater.inflate(R.layout.news_row_center, null, false);
    	
                LinearLayout _citysearchmid = (LinearLayout)_row.findViewById(R.id.LinearLayoutSearchList);
        		TextView _titlemid = (TextView)_row.findViewById(R.id.TextViewTitle);
        		TextView _datemid = (TextView)_row.findViewById(R.id.TextViewPubDate);
        		//ImageView _imagetypemid = (ImageView)_row.findViewById(R.id.ImageView);
        		            	
        		System.out.println("Position: " + position);
        		toShowCent= ((WhyMCANewsModel)this.getItem(position));
        		_titlemid.setText(toShowCent.getTitle());
        		_datemid.setText(toShowCent.getDataTrunk());
        		        		        		
        		_citysearchmid.setOnClickListener(new OnClickListener(){

    			@Override
    			public void onClick(View v) {
    					
    				addSearchPreferences(position);
    					        					 
    				}
    				
    			});
        		
        		break;
        		
        	case END:
        		
    			_row = inflater.inflate(R.layout.news_row_bottom, null, false);
    	
                LinearLayout _citysearchbot = (LinearLayout)_row.findViewById(R.id.LinearLayoutSearchList);
        		TextView _titlebot = (TextView)_row.findViewById(R.id.TextViewTitle);
        		TextView _datebot = (TextView)_row.findViewById(R.id.TextViewPubDate);
        		//ImageView _imagetypebot = (ImageView)_row.findViewById(R.id.ImageView);
        		            
        		System.out.println("Position: " + position);
        		toShowBot = ((WhyMCANewsModel)this.getItem(position));
        		_titlebot.setText(toShowBot.getTitle());
        		_datebot.setText(toShowBot.getDataTrunk());
        		        		        		
        		_citysearchbot.setOnClickListener(new OnClickListener(){

    			@Override
    			public void onClick(View v) {
    					
    				addSearchPreferences(position);
    					        					 
    				}
    				
    			});
        		
        		break;
        		
        	case ONCE:
        		
    			_row = inflater.inflate(R.layout.news_row_once, null, false);
    	
                LinearLayout _citysearchonce = (LinearLayout)_row.findViewById(R.id.LinearLayoutSearchList);
        		TextView _titleonce = (TextView)_row.findViewById(R.id.TextViewTitle);
        		TextView _dateonce = (TextView)_row.findViewById(R.id.TextViewPubDate);
        		        		   
        		System.out.println("Position: " + position);
        		toShowOnce = ((WhyMCANewsModel)this.getItem(position));
        		_titleonce.setText(toShowOnce.getTitle());
        		_dateonce.setText(toShowOnce.getDataTrunk());
        		
        		_citysearchonce.setOnClickListener(new OnClickListener(){

    			@Override
    			public void onClick(View v) {
    					
    				addSearchPreferences(position);
    					        					 
    				}
    				
    			});
        		
        		break;
            		
        }
         		
		return(_row);
		
	}

	@Override
	public void onClick(View v) {
		
		
		
	}

	 private void addSearchPreferences(int position) {
	    	 	
		ViewPost.setPosition(position);
		_viewpost = new Intent(Global.getCtx(), ViewPost.class);
		_viewpost.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Global.getCtx().startActivity(_viewpost);
	    	
	 }
	 	 
	 
	@Override
	public void onDialogClicked(boolean result) {
		// TODO Auto-generated method stub
		
	}
	
}
